
//
//  PermissionsView.m
//  TaiHaoCloud
//
//  Created by rzm on 16/6/8.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxPermissionsView.h"
#import "SampuxAddUserView.h"
#import "SampuxAuthorityTransferView.h"
#import "SampuxAddUserTableViewCell.h"
#import "SampuxSetAliasView.h"
#import "SampuxUnBindView.h"

@interface SampuxPermissionsView ()<UITableViewDelegate,UITableViewDataSource,SendAlisaStrDelegate>

@property (nonatomic, strong) UIButton *authorityTransferBtn;
@property (nonatomic, strong) UIButton *addUserBtn;
@property (nonatomic, strong) SampuxSetAliasView * aliasView;
@property(nonatomic,assign) NSInteger cellSelectedTag;
@property(nonatomic,assign) BOOL swipeGestureStarted;
@property (nonatomic, strong) SampuxUnBindView *deleteUserView;

@end
@implementation SampuxPermissionsView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self loadUI];
        _dataSourceArr = [NSMutableArray array];
    }
    return self;
}

- (SampuxSetAliasView *)setAliasView
{
    if (!_aliasView ) {
        _aliasView = [[SampuxSetAliasView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, self.height - 64)];
        _aliasView.configmodel = self.model;
        _aliasView.aliasType = SampuxSetAliasView_HailFellow;
    }
    return _aliasView;
}

- (SampuxUnBindView *)deleteUserView
{
    if (_deleteUserView == nil) {
        _deleteUserView = [[SampuxUnBindView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, self.height - 64)];
        _deleteUserView.unBindType = SampuxDelete_Type_User;
        _deleteUserView.configmodel = self.model;
        __weak typeof(SampuxPermissionsView *) wSelf = self;
        _deleteUserView.cancelBlock = ^()
        {
            [wSelf cancelDeleteUserView];
        };

    }
    return _deleteUserView;
}

- (void)loadUI
{
     _firendTableView = [[UITableView alloc]initWithFrame: CGRectMake(0, 64, kScreenWidth, self.height - 64)];
    [self addSubview:_firendTableView];
    _firendTableView.delegate = self;
    _firendTableView.dataSource = self;
    _firendTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _firendTableView.backgroundColor = THSystemClearColor;
    [_firendTableView registerNib:[UINib nibWithNibName:@"SampuxAddUserTableViewCell" bundle:nil] forCellReuseIdentifier:@"addCell"];

    _header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    _firendTableView.mj_header = _header;
    [_header beginRefreshing];
}

- (void)refreshData
{
    [GBInterMaster_CommForSystem requestDeviceUserList:0 withDeviceAccount:self.model.deviceAccount];
}

#pragma mark UITabelViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SampuxAddUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addCell"];
    cell.backgroundColor = THWhiteAlplaColor(0.1);
    if (cell == nil) {
        cell = [[SampuxAddUserTableViewCell alloc]init];
    }

    GBInterMaster_Bean_DeviceOperatorInfo *info = _dataSourceArr[indexPath.row];
    cell.userTeleLabel.text = info.userAccount;

    if (info.userAlias.length == 0 ||[info.userAlias isEqualToString:@"\0"]) {
        cell.userNameLabel.text = info.userAccount;
    }else
    {
        cell.userNameLabel.text = info.userAlias;
    }
  
    cell.cellTag = indexPath.row;
    cell.editBlock = ^(NSInteger cellSelectedTag){
        self.cellSelectedTag = cellSelectedTag;
        AppDelegate *delegate = [UIApplication sharedApplication].delegate;
        delegate.window.backgroundColor = THSystemBlackColor;
        [delegate.window addSubview:self.setAliasView];
        self.setAliasView.alisaDelegate = self;
        self.setAliasView.info = info;
    };
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 44, kScreenWidth, 1)];
    [cell addSubview:view];
    view.backgroundColor = THBlackAlplaColor(0.2);
    
    return cell;
}


#pragma mark UITableViewDelegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SampuxAddUserTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundColor = THWhiteAlplaColor(0.2);
    if (self.rowDelegate && [self.rowDelegate respondsToSelector:@selector(seletedHailFellowRow:)]) {
        [self.rowDelegate seletedHailFellowRow:indexPath];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SampuxAddUserTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundColor = THWhiteAlplaColor(0.1);
}

/*行的移动方法*/

//确认编辑类型
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;//设置编辑风格为删除风格
}

//删除cell方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    SampuxAddUserTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSUInteger row = [indexPath row]; //获取当前行
    GBInterMaster_Bean_DeviceOperatorInfo *info = _dataSourceArr[row];
    self.deleteUserView.userAccount = info.userAccount;
    [self deleteEquipmentViewShow];
    __weak typeof(SampuxPermissionsView *) wSelf = self;
    self.deleteUserView.deleteBlock = ^(){
        dispatch_async(dispatch_get_main_queue(), ^{
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            [wSelf.dataSourceArr removeObjectAtIndex:row]; //在数据中删除当前对象
            [wSelf.firendTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];//数组执行删除操作
        }
        });
    };
  
}

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    self.swipeGestureStarted = YES;
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryNone;
    NSLog(@"begin");
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.swipeGestureStarted) {
        NSLog(@"didEndEditingRowAtIndexPath");
        self.swipeGestureStarted = NO;
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0;
}

- (void)setDataSourceArr:(NSMutableArray *)dataSourceArr
{
    dispatch_async(dispatch_get_main_queue(), ^{
        _dataSourceArr = dataSourceArr;
        [_firendTableView reloadData];
        [_header endRefreshing];
    });
}

- (void)sendAlisaStr:(NSString *)aliasStr
{
    dispatch_async(dispatch_get_main_queue(), ^{
        GBInterMaster_Bean_DeviceOperatorInfo *info = _dataSourceArr[self.cellSelectedTag];
        info.userAlias = aliasStr;
        [_firendTableView reloadData];
   });
}

- (void)cancelDeleteUserView
{
    [self.deleteUserView removeFromSuperview];
}

- (void)deleteEquipmentViewShow
{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    [delegate.window addSubview:self.deleteUserView];
}

@end
